<template>
    <div>
      <el-form ref="form" :model="user" :rules="rules" label-width="80px">
        <el-form-item label="身高" prop="height">
          <el-input v-model="user.height" placeholder="请输入身高"/>
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="user.weight" placeholder="请输入体重"/>
        </el-form-item>
        <el-form-item label="余额" prop="money">
          <el-input v-model="user.money" placeholder="填写余额"/>
        </el-form-item>
        <el-form-item label="收获地址" prop="address">
          <el-input v-model="user.address" placeholder="请填写收获地址"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="close">关闭</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>

  import {hasPerm} from '@/utils/auth'
  import {listUser, getUser, addUser, updateUser, delUser} from '@/business/api/user'

  export default {
    name: 'detail',
    computed: {
      isAdmin() {
        return hasPerm('business:detail:money')
      }
    },
    data: function() {
      return {
        rules: {

        },
        user: {
          height: null,
          weight: null,
          money: null,
          address: null
        },
        currentUserInfo: null,
        // 决定当前是新增还是修改逻辑
        isAdd: true
      }
    },
    async created() {
      // 获取当前登录的用户信息
      await this.$store.dispatch("GetInfo").then((response) => {
        this.currentUserInfo = response.user;
      });
      // 获取用户的详细信息
      await getUser(this.currentUserInfo.userId).then(response => {
        if (response.code === 200) {
          if (Reflect.has(response, 'data')) {
            this.user = response.data;
            this.isAdd = false;
          }else {
            this.user = this.resetUser();
            this.isAdd = true;
          }
        }
      })
    },
    methods: {
      addMoney() {

      },
      resetUser() {
        return {
          id: this.currentUserInfo.userId,
          height: null,
          weight: null,
          money: null,
          address: null
        }
      },
      submit() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.isAdd) {
              addUser(this.user).then(response => {
                if (response.code === 200) {
                  this.$message.success('保存成功');
                }else {
                  this.$message.error('保存失败');
                }
              })
            }else {
              updateUser(this.user).then(response => {
                if (response.code === 200) {
                  this.$message.success('保存成功');
                }else {
                  this.$message.error('保存失败');
                }
              })
            }
          }
        });
      },
      close() {
        this.$tab.closePage();
      }
    }
  }
</script>

<style scoped>

</style>
